package com.drinks.yiniang.adapter.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.category.CategoryJson;
import com.drinks.yiniang.entity.comment.BannerEntity;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.utils.CommonUtils;

import java.util.List;

/**
 * 分类banner adapter
 *
 * Created by han on 2019/6/27 0027.
 */

public class CategoryPageBannerAdapter extends BaseAdapter {
    private Context mContext;

    private LayoutInflater inflater;

    private List<BannerEntity> mList;
    private BannerEntity mData;

    private StringBuilder mBuilder;

    public CategoryPageBannerAdapter(Context mContext, List<BannerEntity> mList) {
        this.mContext = mContext;
        this.mList = mList;

        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return CommonUtils.getListSize(mList);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;

        if (convertView == null) {
            mHolder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_category_page_banner, null);

            mHolder.iv_category_banner_cover = convertView.findViewById(R.id.iv_category_banner_cover);
            mHolder.tv_category_banner_name = convertView.findViewById(R.id.tv_category_banner_name);

            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        mData = mList.get(position);

        GlideUtils.loadSquareImage(mContext, mHolder.iv_category_banner_cover, mData.getCover_url());

        mHolder.tv_category_banner_name.setText(mData.getTitle());

        return convertView;
    }

    class ViewHolder {
        //name
        TextView tv_category_banner_name;
        //cover
        RatioRoundImageView iv_category_banner_cover;
    }

    public List<BannerEntity> getList() {
        return mList;
    }

    public void addBannerList(List<BannerEntity> banners) {
        if(mList == null || CommonUtils.isEmptyList(banners)) {
            return;
        }

        mList.addAll(banners);

        notifyDataSetChanged();
    }
}
